package syf.singlelist;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class SingleLinkList{
    @Autowired
    private HeroNode head;

    public SingleLinkList(HeroNode head) {
        this.head = head;
    }

    //添加节点
    public void addNode(HeroNode heroNode){
        HeroNode temp=head;
        while (true){
            if (null==temp.next)
                break;
            temp=temp.next;
        }
        temp.next=heroNode;
    }
    //有序添加节点
    public void addNodeByOrder(HeroNode heroNode) {
        HeroNode temp = head;
        while (true) {
            if (null == temp.next) {
                temp.next=heroNode;
                break;
            }
            else {
                if (temp.next.no>heroNode.no){
                    heroNode.next=temp.next;
                    temp.next = heroNode;
                    break;
                }
                else if (temp.next.no==heroNode.no) {
                    System.out.println("编号已存在，不能添加");
                    break;
                }
                else temp = temp.next;
            }
        }
    }
    //修改节点,编号不能改
    public void Modify(HeroNode heroNode){
        HeroNode temp=head;
        while (true){
            if (null==temp.next){
            System.out.println("没有该节点信息");
            break;
            }
            if (temp.next.no==heroNode.no) {
                temp.next.name = heroNode.name;
                temp.next.NikeName= heroNode.NikeName;
                break;
            } else{
                temp=temp.next;
            }
        }
    }
    public void Delete(HeroNode heroNode){
        HeroNode temp=head;
        while (true){
            if (null==temp.next){
                System.out.println("没有该节点信息");
                break;
            }
            if (temp.next.no==heroNode.no&&temp.next.next!=null) {
                temp.next = temp.next.next;
                break;
            }else if (temp.next.no==heroNode.no&&temp.next.next==null){
                temp.next=null;break;
            }
            else{
                temp=temp.next;
            }
        }
    }
    //打印节点信息
    public void PrintList(){
        HeroNode temp=head;
        while (true){
            if (null==temp.next){
                break;
            }
            System.out.println(temp.next);
            temp=temp.next;
        }
    }
    //打印节点个数
    int PrintCount(){
        int count=0;
        HeroNode temp=head;
        while (true){
            if (null==temp.next){
                break;
            }
            count++;
            temp=temp.next;
        }
        return count;
    }
    //打印倒数第k节点信息
    public void PrintK(int k){
        int count=this.PrintCount()+1;
        if (count==1){
            System.out.println("没有");
            return;}
        int index=0;
        HeroNode temp=head;
        while (true){
            index++;
            if (k<0||k>count){
                System.out.println("错误的输入或总共没有这么多节点");break;}
            else if (index==count-k){
                System.out.println("倒数第"+k+"个为:"+temp.next);
            break;
            }
            temp=temp.next;
        }
    }
    public HeroNode getHead(){
        return this.head;
    }
    public void reverseHead( ){
        HeroNode reversehead=new HeroNode(null,"","");
        HeroNode temp=head;
        HeroNode zhongjian;
        while (true){
            if (temp.next==null)
                break;
            zhongjian=temp.next;
            temp.next=temp.next.next;
            zhongjian.next=reversehead.next;
            reversehead.next = zhongjian;
        }
        this.head=reversehead;
    }
    public void converge(HeroNode newhead){
        HeroNode temp=head;
        HeroNode trans;
        while (true){
            if (null==newhead.next||null==temp.next){
                temp.next=newhead.next;
                break;}
            if (newhead.next.no<temp.next.no) {
                trans=newhead.next;
                newhead.next=newhead.next.next;
                trans.next=temp.next;
                temp.next=trans;
                temp=head;
            }
             temp=temp.next;
        }
    }
    @Override
    public String toString() {
        return "SingleLinkList{" +
                "head=" + head +
                '}';
    }

}

